﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;

namespace TfsBacklogMgr.Main
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        IFactory _factory;

        public MainWindow()
        {
            InitializeComponent();

            // Factory.IsTesting = true;

            _factory = Factory.GetFactory();

            var ctx = Context.GetContext();
            DataContext = ctx;
        }

        private void ConnectToTfs_Click(object sender, RoutedEventArgs e)
        {
            BusyWarning.IsBusy = true;
            Tfs.ConnectionInfo conn = Tfs.TfsServer.SelectProject();
            if (conn != null)
            {

                Context.GetContext().TfsProject = conn.TeamProject;
                Context.GetContext().TfsServer = conn.TeamProjectCollectionUri.ToString();
                _factory.ConnectToTfs(); 
                // triggers context changes
            }
            BusyWarning.IsBusy = false;
        }

        private void User_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox listbox = sender as ListBox;
            if (listbox != null && listbox.SelectedItems.Count > 0)
            {
                Context.GetContext().CurrentUser = listbox.SelectedItems[0] as User;
                if (Context.GetContext().CurrentUser != null)
                {
                    UserDetails details = new UserDetails(Context.GetContext());
                    details.ShowDialog();
                }
            }
        }

        private void MainWindow_IsClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Context ctx = Context.GetContext();
            if (string.IsNullOrEmpty(ctx.Filename) && ctx.TfsIsConnected)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Title = "Save Backlog Manager settings";
                dlg.AddExtension = true;
                dlg.DefaultExt = ".backlog";
                dlg.Filter = "Backlog Mgr File(*.backlog)|*.backlog|All files (*.*)|*.*";
                if (dlg.ShowDialog() == true)
                {
                    ctx.Filename = dlg.FileName;
                }
            }
        }
        private void MainWindow_Closing(object sender, EventArgs e)
        {
            Context ctx = Context.GetContext();
            if (!string.IsNullOrEmpty(ctx.Filename))
            {
                _factory.SaveSettings(ctx);
                WorkItem.SaveAll();
            }
        }

        private void SelectUsers_Click(object sender, RoutedEventArgs e)
        {
            ChooseUsers w = new ChooseUsers(Context.GetContext());
            w.ShowDialog();
            Context.GetContext().UpdateUsers();
        }

        private void SelectIterations_Click(object sender, RoutedEventArgs e)
        {
            ChooseIterations w = new ChooseIterations(Context.GetContext());
            w.ShowDialog();
            Context.GetContext().AddIterations();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var ctx = Context.GetContext();
            if (!string.IsNullOrWhiteSpace(ctx.Filename))
            {
                BusyWarning.IsBusy = true;
                ctx = _factory.LoadSettings(ctx.Filename);
                BusyWarning.IsBusy = false;
            }
        }


    }
}
